package com.kucun.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.sdlc.com.database.BaseDaoInterface;

import org.sdlc.com.database.impl.MysqlBaseDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.kucun.bean.Page;
import com.kucun.bean.Clientinfo;
import com.kucun.bean.Users;
import com.kucun.service.ClientinfoService;
import com.kucun.util.GsonUtils;
import com.kucun.util.UID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/clientinfo")
public class ClientinfoController {

	@Autowired
	private ClientinfoService clientinfoService;

	
	/**
	 * 
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping("/getallClientinfo.html")
	public String getAllUser(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
        String q=request.getParameter("q");
        //System.out.println("q="+q);
		String field=request.getParameter("field");
		
		if(q!=null){
			field="clientname";
		}
        String searchertext=request.getParameter("searchertext");
        if("".equals(searchertext)||searchertext==null){
        	searchertext=q;
        }else{
        	searchertext = URLDecoder.decode(searchertext, "utf-8");
        } 
		Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		page1.setFiled(field);
		page1.setSearchertext(searchertext);
		try {
			String a=request.getParameter("a");
			page1.setObj(a);
		} catch (Exception e) {
			// TODO: handle exception
		}
		Map<String, Object> jsonMap = new HashMap<String, Object>(); 
		List<Clientinfo> list = clientinfoService.findAll(page1);	
		jsonMap.put("total",clientinfoService.getCount(page1).getTotal());
        jsonMap.put("rows",list); 
        JSONObject jsonObject = JSONObject.fromObject(jsonMap);
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	
	@RequestMapping("/getallclient.html")
	public String getAll(int rows,int page,HttpServletRequest request,HttpServletResponse response) throws IOException{
        Page page1=new Page();
		page1.setI(rows*(page-1));
		page1.setRows(rows);
		List<Clientinfo> list = clientinfoService.findAll(page1);	
		JSONArray jsonObject = JSONArray.fromObject(list);//鏍煎紡鍖杛esult   涓�瀹氳鏄疛SONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	@RequestMapping("/getallclient1.html")
	public String getAll1(HttpServletRequest request,HttpServletResponse response) throws IOException{
		List<Clientinfo> list = clientinfoService.findAll();	
		JSONArray jsonObject = JSONArray.fromObject(list);//格式化result   一定要是JSONObject  
        PrintWriter pw = response.getWriter();
        pw.write(jsonObject.toString());
		return null;
	}
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/addclientinfo.html")
	public String addClientinfo(Clientinfo goods,HttpServletRequest request,HttpServletResponse response) throws IOException{
		String tag=request.getParameter("tag");
		Users user=(Users)request.getSession().getAttribute("user");
		int s=0;
		PrintWriter pw = response.getWriter();
		if("1".equals(tag)){
			int ss=clientinfoService.updateByPrimaryKeySelective(goods);
			if(ss>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}else{
			String billon="WP"+UID.next();
			goods.setId(null);
			goods.setBackup(billon);
			s=clientinfoService.save(goods);
			if(s>0){
				 pw.write("ok");
			}else{
				 pw.write("err");
			}
		}
		return null;
	}
	/**
	 * @param user
	 * @param request
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/updateclinetdel.html")
	public String updateUser(String ids,Clientinfo client,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int s=clientinfoService.updateClientForDel(ids);
		PrintWriter out = response.getWriter();
		if(s>0){
			 out.write("ok");
		}else{
			 out.write("err");
		}
		return null;
	}
	/**
	 * @param id
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/delClientinfo.html")
	public void delClientinfo(Integer id,HttpServletRequest request,HttpServletResponse response) throws IOException{
		System.out.println(id);
		BaseDaoInterface basedao=MysqlBaseDaoImpl.getInstance();
		StringBuffer sb=new StringBuffer();
		//List<Map<String, Object>> s=new TreeNodeService().doTreeData(id,basedao,sb);
		System.out.println(sb);
		String result = "err";
		String ids=sb.toString()+id;
		if(clientinfoService.delClientinfoForDel(ids)>0){
			result = "ok";
		}
		

			PrintWriter out = response.getWriter();
			out.write(result);
		

	}
	
	@RequestMapping("/exit.html")
	public String clear(HttpServletRequest request){
		request.getSession(true).setAttribute("user",null);
		return "/login";
	}
	
}
